
exports.GASprite = GL2.Node.subclass ({
	initialize : function(path, frame, anchor,uvs) {
		
		this._frame = frame;
		this._anchor = anchor;
		this._path = path;
		this._sprite = new GL2.Sprite();
		this._sprite.setImage(this._path, frame, anchor,uvs);

		this.setup();
		
		this._showBoundaries = false;
		this._drawing = null;
	},
	
	boundariesShown: function() {
		return this._showBoundaries;
	},
	
	showBoundaries: function(flag) {
		if (this._drawing != null) {
			if (flag == false) {
				this._drawing.setVisible(false);
			}
			else {
				this.drawBoundaries();
				this._drawing.setVisible(true);
			}
		}
		this._showBoundaries = flag;
	},
	
	drawBoundaries: function() {
		if (this._drawing == null) {
			this._drawing = new GL2.Primitive();
			this._drawing.setType(3);
			this._sprite.addChild(this._drawing);
		}
		var totalVertices = 2 * this._totalPoints;
		for(var i=0; i < this._totalPoints; i++) {
			var vertex1 = new Core.Point(this._points[i].x, this._points[i].y);
			var vertex2 = new Core.Point(this._points[(i+1) % this._totalPoints].x, this._points[(i+1) % this._totalPoints].y);
			if(this._drawing.getVertexCount() < totalVertices) {
				//console.log("x1:" + vertex1.getX() + ", y1:" + vertex1.getY() + ", x2:" + vertex2.getX() + ", y2:" + vertex2.getY());
				this._drawing.pushVertex(new GL2.Primitive.Vertex(vertex1, [0, 0], [1, 0, 0]));
				this._drawing.pushVertex(new GL2.Primitive.Vertex(vertex2, [0, 0], [1, 0, 0]));
			}
			else {
				this._drawing.setVertex(2*i, new GL2.Primitive.Vertex(vertex1, [0, 0], [1, 0, 0]));
				this._drawing.setVertex(2*i+1, new GL2.Primitive.Vertex(vertex2, [0, 0], [1, 0, 0]));
			}
		}
	},
	
	getNode : function () {
		return this._sprite;
	},
	
	setup: function() {
		Storage.FileSystem.readFile(this._path + ".json", this.readComplete.bind(this));
	},
	
	readComplete: function(error, contents) {
		if (!error && contents != null) {
			this._points = eval("(" + contents + ")");
			this._totalPoints = this._points.length;
			
			var factorX = this._frame[0] * this._anchor[0];
			var factorY = this._frame[1] * this._anchor[1];
			var imageHeight = this._frame[1];
			for (var i = 0; i < this._totalPoints; i++) {
				var point = this._points[i];
				point.x = point.x - factorX;
				point.y = imageHeight - point.y - factorY;
			}
			
			
			if (this._showBoundaries == true) {
				this.drawBoundaries();
			}
		}
		else {
			console.log("GASprite: ERROR READING JSON, ERROR: " + error + ", PATH: " + this._path);
		}
	},
	
	isTouched: function(location) {
		location = this._sprite.screenToLocal(location);
		var i = 0;
		var j = this._totalPoints - 1;
		var x = location.getX();
		var y = location.getY();
		var oddNodes = false;
		
		for (i = 0; i < this._totalPoints; i++) {
			var pointi = this._points[i];
			var pointj = this._points[j];
			if ( (pointi.y < y && pointj.y >= y) || (pointj.y < y && pointi.y >= y) ) {
				if ( pointi.x + (y - pointi.y) / (pointj.y - pointi.y) * (pointj.x - pointi.x) < x ) {
					oddNodes = !oddNodes;
				}
			}
			j = i;
		}
		return oddNodes;
	},
	
	destroy : function () {
		if (typeof this._drawing != 'undefined') {
			this._drawing.destroy();
			this._drawing = null;
		}
		this._sprite.destroy();
		delete this._points;
	}
});